package net.sf.rose.business.action.define;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.rose.business.bean.control.ViewBean;
import net.sf.rose.business.service.ViewService;
import net.sf.rose.jdbc.UserBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.OnlineUserListener;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 功能：
 * 
 * @author fengjian
 * @since 2014/01/29
 */
@Controller
@RequestMapping("/define/view")
public class ViewController {

	private static String LIST_PAGE = "define/view/list";
	private static String VIEW_PAGE = "define/view/edit";

	/**
	 * 普通查询
	 */
	@RequestMapping("/list.html")
	public ModelAndView list(HttpServletRequest request,Service service) {
		//request.getParameterMap();
		String[] sorts = getViewSorts(request);
		ModelAndView mav = new ModelAndView(LIST_PAGE);
		mav.addObject("sorts", sorts);
		mav.addObject("sort", "all");
		return mav;
	}

	private String[] getViewSorts(HttpServletRequest request){
		String[] array = null;
		Object obj = request.getSession().getAttribute("define_view_sorts");
		if (obj == null) {
			Map<String, ViewBean> bean = ViewService.getAll();
			Set<String> sorts = new HashSet<String>();
			for (ViewBean a : bean.values()) {
				sorts.add(a.getSort());
			}
			array = new String[sorts.size()];
			Object[] objs = sorts.toArray();
			for (int i = 0; i < sorts.size(); i++) {
				array[i] = objs[i].toString();
			}
			request.getSession().setAttribute("define_view_sorts", array);
		} else {
			array = (String[]) obj;
		}
		return array;
	}

	/**
	 * 普通查询
	 */
	@RequestMapping("/search.html")
	public ModelAndView search(HttpServletRequest request,String sort,String viewName) {
		Map<String, ViewBean> map = this.getViews(sort, viewName);
		String[] sorts = getViewSorts(request);
		ModelAndView mav = new ModelAndView(LIST_PAGE);
		mav.addObject("list", map);
		mav.addObject("sorts", sorts);
		mav.addObject("sort", sort);
		mav.addObject("actionName", viewName);
		return mav;
	}

	private Map<String, ViewBean> getViews(String sort,String viewName){
		Map<String, ViewBean> map = null;
		Map<String, ViewBean> bean = ViewService.getAll();
		if (sort == null || sort.equals("all")) {
			map = bean;
		} else {
			map = new HashMap<String, ViewBean>();
			for (String key : bean.keySet()) {
				ViewBean a = bean.get(key);
				if (a.getSort().equals(sort)) {
					map.put(key, a);
				}
			}
		}
		if (viewName != null && !viewName.trim().equals("")) {
			String name = viewName.trim();
			Map<String, ViewBean> m = new HashMap<String, ViewBean>();
			for (String key : map.keySet()) {
				ViewBean a = map.get(key);
				if (a.getName().indexOf(name) != -1) {
					m.put(key, a);
				}
			}
			map = m;
		}
		return map;
	}

	/**
	 * 
	 */
	@RequestMapping("/add.html")
	public ModelAndView add(HttpServletRequest request) {
		ViewBean bean = new ViewBean();
		ModelAndView mav = new ModelAndView(VIEW_PAGE);
		mav.addObject("bean", bean);
		return mav;
	}

	/**
	 * 
	 */
	@RequestMapping("/get.html")
	public ModelAndView get(HttpServletRequest request, String name) {
		ViewBean bean = ViewService.getAll().get(name);
		ModelAndView mav = new ModelAndView(VIEW_PAGE);
		mav.addObject("bean", bean);
		return mav;
	}

	/**
	 * 
	 */
	@RequestMapping("/delete.html")
	public ModelAndView delete(HttpServletResponse response, String name) throws Exception {
		ViewService.getAll().remove(name);
		response.reset();
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = response.getWriter();
		pw.write("{\"msg\":\"删除成功！\"}");
		pw.flush();
		pw.close();
		return null;
	}

	/**
	 * 提交操作
	 */
	@RequestMapping("/save.html")
	public ModelAndView save(HttpServletRequest request,ViewBean bean) {
		ViewService.getAll().put(bean.getName(), bean);
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		return mav;
	}

	/**
	 * 提交操作
	 */
	@RequestMapping("/saveAll.html")
	public ModelAndView saveAll(HttpServletRequest request) {
		UserBean user = OnlineUserListener.getOnlineUser(request);
		ViewService.save(user);
		ModelAndView mav = null;
		mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		return mav;
	}
}
